package crawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import asa.PostBucket;

import parser.ParserAnnotation;
import parser.ParserPipeline;

import forum.ForumFactory;
import forum.ForumPost;

public class StdinCrawler extends Crawler {
	private BufferedReader br;
	private boolean canCrawl = true;

	public StdinCrawler(ForumFactory fac, ParserPipeline pp, PostBucket bucket) {
		super("Stdin", fac, pp, bucket);
		br = new BufferedReader(new InputStreamReader(System.in));
	}

	@Override
	public boolean canCrawl() {
		return canCrawl;
	}

	/**
	 * Reads a line from stdin.
	 */
	@Override
	public ArrayList<ForumPost> crawl() throws IOException {
		ArrayList<ForumPost> ft = new ArrayList<ForumPost>();
		String line = br.readLine();
		if (line == null) {
			line = "";
			canCrawl = false;
			return null;
		}
		ParserAnnotation pa = pp.make(line);
		ForumPost post = fac.makePost(line, pa, "stdin");
		ft.add(post);
		return ft;
	}

	@Override
	public void close() throws Exception {
	}
}
